/**
 * Copyright (c) 2012-2014 All Rights Reserved.
 */
package com.easy.framwork.jdbc.handlers;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 * 
 * @author wy
 * @version v 0.1 2014-1-2 下午9:14:18 wy Exp $
 */
public class SingleValueHandler implements ResultSetHandler<Object> {

    /**
     * @see com.easy.framwork.jdbc.handlers.ResultSetHandler#handle(java.sql.ResultSet)
     */
    @Override
    public Object handle(ResultSet rs) throws SQLException {

        ResultSetMetaData rsmd = rs.getMetaData();
        int cols = rsmd.getColumnCount();
        if (cols > 1) {
            throw new SQLException("ResultSet column count than 1");
        }

        Object object = null;
        int row = 0;
        while (rs.next()) {
            row++;
            object = rs.getObject(1);
        }
        if (row > 1) {
            throw new SQLException("ResultSet row count than 1");
        }
        return object;

    }

}
